package com.skivingcloud.workflow.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.workflow.entity.Strategy;
import com.skivingcloud.workflow.service.IStrategyService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 找人策略 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/workflow/strategy")
public class StrategyController {

	protected static final Logger log = LoggerFactory.getLogger(StrategyController.class);

	@Autowired
	private IStrategyService strategyService;

	/**
	 * 分页查询策略
	 * 
	 * @param params
	 * @return
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		PageUtils page = strategyService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 新增策略
	 * 
	 * @param strategy
	 * @return
	 */
	@PostMapping("/save")
	public R save(@RequestBody Strategy strategy) {
		try {
			User user = UserUtil.getUser();
			strategyService.save(strategy, user);
			return R.ok();
		} catch (Exception e) {
			log.error("保存策略失败", e);
			return R.error("保存策略失败");
		}
	}

	/**
	 * 根据编码获取策略信息
	 * 
	 * @param strategyCode
	 * @return
	 */
	@GetMapping("/getStrategyIdByCode")
	public R getStrategyIdByCode(@PathVariable("strategyCode") String strategyCode) {
		Strategy strategy = strategyService.getStrategyIdByCode(strategyCode);
		return R.ok().put("strategy", strategy);
	}

	/**
	 * 根据id获取策略信息
	 * 
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		Strategy strategy = strategyService.selectById(id);
		return R.ok().put("strategy", strategy);
	}

	/**
	 * 修改策略配置信息
	 */
	@PostMapping("/update")
	public R update(@RequestBody Strategy strategy) {
		try {
			User user = UserUtil.getUser();
			strategyService.update(strategy, user);
			return R.ok();
		} catch (Exception e) {
			log.error("保存策略失败", e);
			return R.error("保存策略失败");
		}
	}

	/**
	 * 删除策略信息
	 * 
	 * @param ids
	 * @return
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			strategyService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除策略失败", e);
			return R.error("删除策略失败");
		}
	}

	/**
	 * 
	 * @return
	 */
	@GetMapping("/strategyOption")
	public List<Strategy> strategyOption() {
		List<Strategy> result = strategyService.select(new Strategy());
		return result;
	}
}
